/*
 * PagoscomerciosFijosFrame.java
 *
 * Created on 13 de abril de 2008, 11:44 AM
 */
package org.remolques.tipos;


import org.mybeans.Sesion;
 
import org.mybeans.ComerciosBeans;
import org.mybeans.ContribuyentesBeans;
import org.mybeans.RemolquesBeans;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import org.jdesktop.swingx.autocomplete.*;

/**
 *
 * @author  avbravo
 */
public class RemolquesTipoActualizarFrame extends javax.swing.JFrame {

    Sesion sesion;
    int IdRemolqueTipo;
    String RemolqueTipo;

    /** Creates new form */
    public RemolquesTipoActualizarFrame() {
        initComponents();
        sesion = new Sesion();
        /*
         * Fondo
         */

         
         
         

        jPanelFondo.setOpaque(false);
        setContentPane(jPanelFondo);
        /*
         * fondo
         */

        sesion.setTipoId("remolquestipo");



        AutoCompleteDecorator.decorate(this.jComboBoxRemolqueTipo);
        CargarComboRemolquesTipo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelFondo = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jButtonEliminar = new javax.swing.JButton();
        jButtonRegresar2 = new javax.swing.JButton();
        jComboBoxRemolqueTipo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldRemolqueTipo = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ACTUALIZAR TIPOS DE REMOLQUES");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanelFondo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelFondo.setOpaque(false);
        jPanelFondo.setPreferredSize(new java.awt.Dimension(1007, 800));

        jLabel4.setText("Tipo Remolque");

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/save.png"))); // NOI18N
        jButtonEliminar.setToolTipText("Actualizar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jButtonRegresar2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar2.setToolTipText("Regresar");
        jButtonRegresar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresar2ActionPerformed(evt);
            }
        });

        jComboBoxRemolqueTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxRemolqueTipoActionPerformed(evt);
            }
        });

        jLabel1.setText("Nuevo Nombre");

        javax.swing.GroupLayout jPanelFondoLayout = new javax.swing.GroupLayout(jPanelFondo);
        jPanelFondo.setLayout(jPanelFondoLayout);
        jPanelFondoLayout.setHorizontalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxRemolqueTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jButtonRegresar2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldRemolqueTipo)))
                .addContainerGap(70, Short.MAX_VALUE))
        );
        jPanelFondoLayout.setVerticalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBoxRemolqueTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldRemolqueTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRegresar2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonRegresar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresar2ActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresar2ActionPerformed

private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed


    try {
        if (ValidarEntradas() == false) {
            return;
        }

        ActualizarTipoRemolque();

        CargarComboRemolquesTipo();
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Metodo:  jButtonImprimirActionPerformed()\n " + ex.getMessage().toString());
    }

}//GEN-LAST:event_jButtonEliminarActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
}//GEN-LAST:event_formWindowGainedFocus

private void jComboBoxRemolqueTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxRemolqueTipoActionPerformed
    // TODO add your handling code here:
    try {
        String temp = (String) jComboBoxRemolqueTipo.getSelectedItem();
        if (temp == null || temp.equals("")) {
            return;
        }
        temp = temp.trim();
        int posicion = temp.indexOf("|");

        int posfinal = 0;
        if (posicion <= 0) {
            posfinal = 0;
        } else {
            posfinal = posicion - 1;
        }

        String remolquetipo = temp.substring(0, posfinal);
        remolquetipo = remolquetipo.trim();
        jTextFieldRemolqueTipo.setText(remolquetipo);
        int largo = temp.length();

        int inic = posfinal + 2;
        String sIdRemolqueTipo = temp.substring(inic, largo);
        sIdRemolqueTipo = sIdRemolqueTipo.trim();
        this.IdRemolqueTipo = Integer.parseInt(sIdRemolqueTipo);

        /*
         *
         */

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error  " + ex.getMessage(), "ComboBox Regimen ActionPerformed", JOptionPane.ERROR_MESSAGE);
    }

}//GEN-LAST:event_jComboBoxRemolqueTipoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RemolquesTipoActualizarFrame().setVisible(true);
            }
        });
    }

 

    private boolean ValidarEntradas() {
        try {

            String sIdRemolque = String.valueOf(this.IdRemolqueTipo);

            if (sIdRemolque == null || sIdRemolque.equals("")) {
                JOptionPane.showMessageDialog(this, "No ha seleccionado un remolque", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jComboBoxRemolqueTipo.requestFocus();
                return false;
            }
            this.RemolqueTipo = jTextFieldRemolqueTipo.getText();
            this.RemolqueTipo = this.RemolqueTipo.trim();

            if (this.RemolqueTipo == null || this.RemolqueTipo.equals("")) {
                JOptionPane.showMessageDialog(this, "Indique el nuevo nombre del tipo de remolque", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldRemolqueTipo.requestFocus();
                return false;
            }


            return true;
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "Clase: PagosComerciosEventualesFrame\nValidarEntradas()\n " + ex.getMessage().toString());
        }
        return false;
    }

    private void ActualizarTipoRemolque() {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String sql = "update  remolquestipo set remolquetipo = ? where idmunicipio=? and idremolquetipo= ? ";
            PreparedStatement prest = sesion.con.prepareStatement(sql);
            prest.setString(1, this.RemolqueTipo);
            prest.setString(2, sesion.getIdmunicipio());
            prest.setInt(3, this.IdRemolqueTipo);

            prest.executeUpdate();
            sesion.con.close();
            JOptionPane.showMessageDialog(this, " Actualizado exitosamente", "Actualizado", JOptionPane.INFORMATION_MESSAGE);



        } catch (Exception ex) {
            System.out.println("Error" + ex);
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

    }

    private void CargarComboRemolquesTipo() {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String sqlquery = "select idremolquetipo, remolquetipo from remolquestipo where idmunicipio='" + sesion.getIdmunicipio() + "' and activo='si' order by remolquetipo";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            ResultSet rs = psquery.executeQuery();
            jComboBoxRemolqueTipo.removeAllItems();
            int count = 0;
            while (rs.next()) {
                String idremolque = rs.getString("remolquetipo");
                idremolque += " | " + rs.getString("idremolquetipo");


                jComboBoxRemolqueTipo.insertItemAt(idremolque, count);
                count++;
            }
            if (count != 0) {
                jComboBoxRemolqueTipo.setSelectedIndex(0);
            }


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error. CargarSubGrupoRegimen()", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonRegresar2;
    private javax.swing.JComboBox jComboBoxRemolqueTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanelFondo;
    private javax.swing.JTextField jTextFieldRemolqueTipo;
    // End of variables declaration//GEN-END:variables
}
